package com.xbongbong.formula.qlexpress.operator.date;

import com.ql.util.express.Operator;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 返回日期date的年份
 * @author 吴峰
 * @version v1.0
 * @date 2019/8/5 10:47
 * @since v1.0
 */
public class OperatorMinute extends Operator {


    public OperatorMinute(String name){
        this.name = name;
    }

    public OperatorMinute(String aliasName, String aName, String errorInfo){
        this.aliasName = aliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }


    /**
     * 语法：MINUTE(time)
     * 说明：返回日期time的分钟部分
     * 例如：录入【任务时间】，通过DAY函数自动填入【具体分钟】。
     * @param list
     * list[0] 起始时间
     * list[1] 结束时间
     * @return
     * @throws Exception
     */
    @Override
    public Object executeInner(Object[] list) throws XbbException {
        if (Objects.isNull(list)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261007);
        }
        if (list[0] == null || StringUtils.equals(list[0].toString(), Constant.ZERO_STRING)) {
            return null;
        }
        Long startTime = PaasDateUtil.getInt(list[0]);
        return DateTimeUtil.getMinuteSomeday(startTime);
    }

}
